package com.baizhi.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.Random;

/*
pom.xml
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
        <version>4.5.16</version>
    </dependency>
*/
public class SendSmsUtil {

    private static String accessKeyId="LTAI5t6d91zGvvtkTr3gD7wY";
    private static String accessKeySecret="x1dDAQOVSE3WE8g91tgpD1qazuTnIb";
    private static String signName="小蛋黄";
    private static String templateCode="SMS_171112532";

    /**
     * 发送手机验证码
     * 参数：手机号,验证码随机数
     * */
    public static String sendPhoneCode(String phoneNumbers,String randomCode){
        //修改公钥和私钥
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        /**修改以下内容*/
        //修改要接受验证码的手机号码
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        //配置已审核通过的签名
        request.putQueryParameter("SignName", signName);
        //配置已审核通过的签模板code
        request.putQueryParameter("TemplateCode", templateCode);
        //配置code变量的值
        request.putQueryParameter("TemplateParam", "{\"code\":\""+randomCode+"\"}");

        CommonResponse response =null;
        try {
            response = client.getCommonResponse(request);
            //System.out.println(response.getData());

        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response.getData();
    }


    public static String sendPhone(String phoneNumbers,String randomCode){

        //调用发送验证码的方法返回信息
        String message = sendPhoneCode(phoneNumbers, randomCode);

        //将字符串转为json对象
        JSONObject jsonObject = JSON.parseObject(message);

        //获取发送手机号的返回信息
        String code = (String) jsonObject.get("Code");

        System.out.println("---"+code);

        String codeMsg="验证码发送失败";

        if(code.equals("OK")) codeMsg="发送成功";
        if(code.equals("isv.OUT_OF_SERVICE")) codeMsg="业务停机";
        if(code.equals("isv.MOBILE_NUMBER_ILLEGAL")) codeMsg="非法手机号";
        if(code.equals("isv.MOBILE_COUNT_OVER_LIMIT")) codeMsg="手机号码数量超过限制";

        return codeMsg;
    }

    //生成随机数
    public static  String  getRandom(int n){
        char[] code =  "0123456789".toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(code[new Random().nextInt(code.length)]);
        }
        return sb.toString();
    }

    public static void main(String[] args) {

        //接收前台传递的手机号
        String phone="13994324736";

        //获取随机数
        String random = getRandom(6);

        //将随机数存储redis
        System.out.println(random);

        //发送验证码
        String msg = sendPhoneCode(phone, random);

        //将字符串转为json对象
        JSONObject jsonObject = JSON.parseObject(msg);

        //获取发送手机号的返回信息
        String code = (String) jsonObject.get("Code");

        System.out.println(code);
    }
}